<?php
/**
 * Created by PhpStorm.
 * User: 王柯 <wangke.email@foxmail.com>
 * Date: 2018\5\5 0005
 * Time: 10:21
 */
namespace app\admin\controller;

class Log extends UserAuth{
    /**
     * 日志管理
     * @author 王柯 <wangke.email@foxmail.com>
     * @return mixed|void
     */
    public function index(){

    }

    /**
     * 日志信息列表
     * @param int id 用户id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View|string|\think\response\Json|\think\response\View|void
     */
    public function log_list(){
        //日志文件目录
        $logDir = LOG_PATH;

        if(is_dir($logDir)){

            $dirName = scandir($logDir);

            $dirArr = [];

            foreach($dirName as $key => $val){
                if($val != '.' && $val != '..'){
                    $dirArr[] = $val;
                }
            }

        }else{
            exit('暂无日志文件！');
        }

        //最新日志日期
        $latest_time = $dirName[array_keys($dirName)[count(array_keys($dirName)) -1]];

        //目录下文件(默认最新文件)
        $files = scandir(LOG_PATH.$latest_time);

        $file = [];

        if(!empty($files)){
            foreach($files as $key => $val){
                if($val != '.' && $val != '..'){
                    $file[$key]['file_name'] = $val;
                    $file[$key]['dir_date'] = $latest_time;
                    $file[$key]['file_path'] = LOG_PATH.$latest_time.'/'.$val;
                }
            }
        }else{
            exit('文件夹为空！');
        }

        if(request()->isPost()){
            $data = input('param.');

            //最新日志日期
            $latest_time = $data['dir_date'];

            //目录下文件(默认最新文件)
            $files = scandir(LOG_PATH.$latest_time);

            $file = [];

            if(!empty($files)){
                foreach($files as $key => $val){
                    if($val != '.' && $val != '..'){
                        $file[$key]['file_name'] = $val;
                        $file[$key]['dir_date'] = $latest_time;
                        $file[$key]['file_path'] = LOG_PATH.$latest_time.'/'.$val;
                    }
                }
            }else{
                exit('文件夹为空！');
            }
        }

        $this->assign([
            'count' => count($file),
            'file' => $file,
            'dir_name' => $dirArr,
        ]);

        return view('log_list');
    }

    /**
     * 读取日志文件
     * @author 王柯 <wangke.email@foxmail.com>
     * @return void
     */
    public function read_log(){
        $data = input('param.');

        //文件路径
        $filePath = LOG_PATH.$data['file_path'];

        /*//预览打开文件
        $file = fopen($filePath,"r");

        //输入文件标签
        header("Content-type: application/pdf");

        //输出文件内容
        dump(fread($file,filesize($filePath)));

        //关闭文件
        fclose($file);*/

        dump(file_get_contents($filePath));
    }

    /**
     * 下载日志
     * @author 王柯 <wangke.email@foxmail.com>
     * @return void
     */
    public function download_log(){
        $data = input('param.');

        //文件路径
        $filePath = LOG_PATH.$data['file_path'];

        //下载文件
        downloadFile($filePath);
    }

    /**
     * 删除日志
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function del_log(){
        if(request()->isAjax()){
            $data = $_POST;

            //文件路径
            $path = LOG_PATH.$data['param'];

            if(is_file($path)){
                $del = unlink($path);
                if($del){
                    return show(config('code.success'),'删除成功！');
                }else{
                    return show(config('code.error'),'删除失败！');
                }
            }else{
                return show(config('code.error'),'文件不存在！');
            }
        }else{
            return show(config('code.error'),'非法请求！');
        }
    }

    /**
     * 批量删除日志
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function batch_del_log(){
        if(request()->isAjax()){
            $data = $_POST;

            if(empty($data['param'])){
                return show(config('code.error'),'请选择要删除的文件！');
            }

            $array = $data['param'];

            foreach($array as $key => $val){
                //文件路径
                $path = LOG_PATH.$val;
                if(is_file($path)){
                    $del = unlink($path);
                    if(!$del){
                        return show(config('code.error'),'删除失败！');
                    }
                }else{
                    return show(config('code.error'),'文件不存在！');
                }
            }

            return show(config('code.success'),'批量删除成功！');
        }else{
            return show(config('code.error'),'非法请求！');
        }
    }
}